<template>
	<view class="container">
		<!-- #ifdef H5 -->
		<uni-header></uni-header>
		<!-- #endif -->

		<view class="bid-box com-box">
			<view class="h2-header">
				<text class="h2-ttl">发布需求</text>
			</view>

			<view class="row">
				<text class="tit">产品名称</text>
				<input class="input" type="text" v-model="goodsName" placeholder="请填写产品名称" />
			</view>
			<view class="row">
				<text class="tit">截止时间</text>
				<view class="value">
					<date-time-picker placeholder="请选择截止时间" fields="minute" @change="goodDeadTime"></date-time-picker>
				</view>
			</view>
			<view class="row">
				<text class="tit">采购数量</text>
				<input class="input" type="number" v-model="num" placeholder="请填写采购数量" />
			</view>
			<view class="row">
				<text class="tit">生产企业</text>
				<input class="input" type="text" v-model="producer" placeholder="请填写生产企业" />
			</view>
			<view class="row">
				<text class="tit">规格参数</text>
				<input class="input" type="text" v-model="specifications" placeholder="请填写规格参数" />
			</view>
			<view class="row">
				<text class="tit">到货时间</text>
				<view class="value">
					<date-time-picker placeholder="请选择到货时间" fields="minute" @change="goodArrivalTime"></date-time-picker>
				</view>
			</view>

			<view class="h2-header">
				<text class="h2-ttl">收货地址</text>
			</view>
			<!-- 地址 -->
			<navigator url="/pages/address/list" class="address-section">
				<view class="order-content">
					<text class="icon icon-region"></text>
					<view class="cen">
						<view class="top">
							<text class="name">{{addressData.name}}</text>
							<text class="mobile">{{addressData.phone}}</text>
						</view>
						<text class="address">{{addressData.provinceName}} {{addressData.cityName}} {{addressData.areaName}}
							{{addressData.address}}
						</text>
					</view>
					<text class="icon icon-arrow_right"></text>
				</view>

				<image class="a-bg" src="/static/arc.png"></image>
			</navigator>
			<button type="primary" class="add-btn" @click="confirm">{{id?'确认修改':'确认发布'}}</button>

		</view>

		<!-- #ifdef H5 -->
		<uni-footer></uni-footer>
		<!-- #endif -->
	</view>
</template>

<script>
	import dateTimePicker from '@/components/uni-datetime-picker.vue'
	import uniHeader from '@/components/h5/uni-header.vue'
	import uniFooter from '@/components/h5/uni-footer.vue'
	export default {
		components: {
			dateTimePicker,
			uniHeader,
			uniFooter
		},
		data() {
			return {
				id: '',
				goodsName: '',
				num: '',
				producer: '',
				specifications: '',
				effectiveTime: '',
				sendTime: '',
				addressData: {
					name: '',
					phone: '',
					provinceName: '',
					cityName: '',
					areaName: '',
					address: '',
					defaultAddress: 1
				}
			}
		},
		onLoad(option) {
			console.log(option.id)
			console.log(typeof(option.id))
			if (option.id) {
				this.id = option.id
				this.goodsName = option.goodsName
				this.num = option.num
				this.producer = option.producer
				this.specifications = option.specifications
			}
			this.$api.request('/zraddress/defaultAddress', {}, {
				method: 'GET',
				content: 'application/x-www-form-urlencoded'
			}).then(res => {
				this.addressData = res.data.address
			})
		},
		methods: {
			goodDeadTime(date) {
				this.effectiveTime = date.year + '-' + date.month2 + '-' + date.day2 + ' ' + date.hour2 + ':' + date.minute
				console.log(this.effectiveTime)
			},
			goodArrivalTime(date) {
				this.sendTime = date.year + '-' + date.month2 + '-' + date.day2 + ' ' + date.hour2 + ':' + date.minute
				console.log(this.sendTime)
			},
			switchChange(e) {
				this.addressData.defaultAddress = e.detail.value ? 1 : 0
				this.addressData.def = e.detail.value
			},
			//提交
			confirm() {
				if (!this.goodsName) {
					this.$api.msg('请填写产品名称')
					return
				}
				if (!this.effectiveTime) {
					this.$api.msg('请填写截止时间')
					return
				}
				if (!this.num) {
					this.$api.msg('请填写采购数量')
					return
				}
				if (!this.producer) {
					this.$api.msg('请填写生产企业')
					return
				}
				if (!this.specifications) {
					this.$api.msg('请填写规格参数')
					return
				}
				if (!this.sendTime) {
					this.$api.msg('请填写到货时间')
					return
				}
				if (!this.addressData.address) {
					this.$api.msg('请填写收货地址')
					return
				}
				let postUrl = '/publishingNeeds/addPublishingNeeds'
				if (this.id) {
					postUrl = '/publishingNeeds/updatePulishingNeed'
				}
				this.$api.request(postUrl, {
					id: this.id,
					goodsName: this.goodsName,
					effectiveTime: this.effectiveTime + ':00',
					num: this.num,
					producer: this.producer,
					specifications: this.specifications,
					sendTime: this.sendTime + ':00',
					sendAddress: this.addressData.addressDetail,
				}).then(res => {
					this.$api.prePage().loadData('refresh')
					uni.navigateBack()
				})
			},
		},
	}
</script>

<style lang="scss">
	page {
		padding-top: 20upx;
	}

	.row {
		.tit {
			width: 150upx;
		}

		.value {
			flex: 1;
		}
	}

	/* #ifdef H5 */
	@media screen and (min-width: 750px) {
		page {
			padding-top: 0;
		}

		.bid-box {
			margin: 20px auto;
		}

		.row {
			.tit {
				width: 150px;
			}
		}
	}

	/* #endif */
</style>
